package cn.com.xy.sms.sdk.ui.popu.part;

import java.lang.ref.WeakReference;
import java.util.Map;

import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import cn.com.xy.sms.sdk.Iservice.XyCallBack;
import cn.com.xy.sms.sdk.smsmessage.BusinessSmsMessage;
import cn.com.xy.sms.sdk.ui.R;
import cn.com.xy.sms.sdk.ui.popu.util.ChannelContentUtil;
import cn.com.xy.sms.sdk.ui.popu.util.SmartSmsSdkUtil;
import cn.com.xy.sms.sdk.ui.popu.util.ThemeUtil;
import cn.com.xy.sms.sdk.ui.popu.util.UIConstant;
import cn.com.xy.sms.sdk.util.DuoquUtils;
import cn.com.xy.sms.sdk.util.JsonUtil;
import cn.com.xy.sms.sdk.util.StringUtils;
import cn.com.xy.sms.util.ParseManager;

public class BubbleBodyCallsMessage extends UIPart {
    private final static String CALL_NUMBER_TEXT = "bodycallmessage_cache_contact_name";
    private TextView mCallUserNameTextView;
    private TextView mCallUserNameTextViewlable;
    private static final int CENTER = Gravity.CENTER;
    private static final int LEFT = Gravity.CENTER_VERTICAL|Gravity.LEFT;
    private static final String TABLE_KEY = "duoqu_table_data_horiz";
    private View mSpliteView;

    public BubbleBodyCallsMessage(Activity context, BusinessSmsMessage message, XyCallBack callback, int layoutId,
            ViewGroup root, int partId) {
        super(context, message, callback, layoutId, root, partId);

    }

    @Override
    public void initUi() throws Exception {
        mCallUserNameTextView = (TextView) mView.findViewById(R.id.duoqu_call_username_title);
        mCallUserNameTextViewlable = (TextView) mView.findViewById(R.id.duoqu_call_username);
        mSpliteView = mView.findViewById(R.id.duoqu_bottom_split_line);
    }

    @Override
    public void setContent(final BusinessSmsMessage message, boolean isRebind) throws Exception {
        this.mMessage = message;
        // 为下面的通用表格布局设置参数
        message.putValue("extend_rows", "1");
        message.putValue("useFirstPadding", false);

        ChannelContentUtil.setViewVisibility(super.mView, View.VISIBLE);
        String cacheName = (String) message.getValue(CALL_NUMBER_TEXT);
        if (StringUtils.isNull(cacheName)) {
            cacheName = ChannelContentUtil.CALLS_MESSAGE_UKNOWN;
        }

        setContentTextAndStyle(cacheName);

        if (!isRebind) {
            ThemeUtil.setTextColor(mContext, mCallUserNameTextView, (String) message.getValue("v_by_text_u_1"),
                    ThemeUtil.getColorId(5010));
            ThemeUtil.setTextColor(mContext, mCallUserNameTextViewlable, (String) message.getValue("v_by_text_d_1"),
                    ThemeUtil.getColorId(3010));
            ThemeUtil.setViewBg(mContext, mView, "", R.drawable.duoqu_v_by_bg_1);
        }

        if(mTask != null){
            mTask.cancel(false);
        }
        mTask = new ContactsTask(this);
        mTask.execute(mMessage);
    }

    private void setContentTextAndStyle(String cacheName) {
        if(mCallUserNameTextViewlable != null && !StringUtils.isNull(cacheName)){
            mCallUserNameTextViewlable.setText(cacheName);
            int length = cacheName.length();
            if(length > ChannelContentUtil.LARGE_CONTENT_SIZE_COUNT){
                if(mCallUserNameTextViewlable.getGravity() != LEFT){
                    mCallUserNameTextViewlable.setGravity(LEFT);
                }
                mCallUserNameTextViewlable.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
            }else{
                if(mCallUserNameTextViewlable.getGravity() != CENTER){
                    mCallUserNameTextViewlable.setGravity(CENTER);
                }
                mCallUserNameTextViewlable.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 32);
            }
        }
        if(mMessage.getTableDataSize(TABLE_KEY) > 0){
            mSpliteView.setVisibility(View.VISIBLE);
        }else{
            mSpliteView.setVisibility(View.GONE);
        }
    }

    AsyncTask mTask = null;
    @Override
    public void destroy() {
        try {
            if (mTask != null) {
                mTask.cancel(false);
            }
        } catch (Throwable e) {
        }
        super.destroy();
    }
    
    public static class ContactsTask extends AsyncTask {
        WeakReference<BubbleBodyCallsMessage> wpart = null;

        public ContactsTask(BubbleBodyCallsMessage p) {
            wpart = new WeakReference<BubbleBodyCallsMessage>(p);
        }

        @Override
        protected BusinessSmsMessage doInBackground(Object... arg) {
            BubbleBodyCallsMessage part = wpart.get();
            if (part == null) {
                return null;
            }
            BusinessSmsMessage msg = (BusinessSmsMessage) arg[0];
            String callNumber = (String) msg.getValue("view_side_phone_num");
            JSONObject contactObj = DuoquUtils.getSdkDoAction().getContactObj(part.mContext, callNumber);
            String name = null;
            if (contactObj == null) {
                name = ChannelContentUtil.CALLS_MESSAGE_UKNOWN;
            } else {
                Object conObj = JsonUtil.getValFromJsonObject(contactObj, UIConstant.CONTACT_TYPE);
                if (conObj != null) {
                    Integer contactType = (int) UIConstant.CONTCAT_TYPE_UNKNOW;
                    try {
                        contactType = (Integer) conObj;
                    } catch (Throwable e) {
                        // TODO: handle exception
                        contactType = (int) UIConstant.CONTCAT_TYPE_UNKNOW;
                    }
                    name = (String) JsonUtil.getValFromJsonObject(contactObj, UIConstant.CONTACT_NAME);
                    if (StringUtils.isNull(name)) {
                        if (UIConstant.CONTCAT_TYPE_FRIEND == contactType) {
                            name = ChannelContentUtil.CALLS_MESSAGE_STRANGER;
                        } else if (UIConstant.CONTCAT_TYPE_UNKNOW == contactType) {
                            name = ChannelContentUtil.CALLS_MESSAGE_STRANGER;
                        } else if (UIConstant.CONTCAT_TYPE_HARASS == contactType) {
                            name = ChannelContentUtil.CALLS_MESSAGE_HARASS;
                        }
                    }
                } else {
                    name = ChannelContentUtil.CALLS_MESSAGE_UKNOWN;
                }
            }
            String cacheName = (String) msg.getValue(CALL_NUMBER_TEXT);
            if (StringUtils.isNull(cacheName) || !cacheName.equals(name)) {
                msg.putValue(CALL_NUMBER_TEXT, name);
                ParseManager.updateMatchCacheManager(part.mMessage);
            }
            return msg;
        }
        
        @Override
        protected void onPostExecute(Object result) {
            BubbleBodyCallsMessage part = wpart.get();
            if (part == null) {
                return;
            }
            if (part.mMessage == result) {
                SmartSmsSdkUtil.smartSdkExceptionLog("AsyncTask result=" + result, null);
                String cacheName = (String) part.mMessage.getValue(CALL_NUMBER_TEXT);
                if (!StringUtils.isNull(cacheName)) {
                    if (!cacheName.equals(part.mCallUserNameTextViewlable.getText().toString())) {
                        part.setContentTextAndStyle(cacheName);
                    }
                }
            }
            part.mTask = null;
        }
    }

    @Override
    public void changeData(Map<String, Object> param) {
        /* SMARTISAN-261 zhaoxiachao 20161129 start */
        if(param != null && !StringUtils.isNull(param.get(ChannelContentUtil.CHANGE_TRAIN)+"")){
            return;
        }
        /* SMARTISAN-261 zhaoxiachao 20161129 end */
    }
}
